﻿using UnityEngine;
using System.Collections;
using LitJson;
using System.Collections.Generic;

public class ShopMenuLogic : SQMenuBase
{
    public Transform _shopMenu;
    private static string path = "Client/UI640/Shop_ChildMenu/shopItem";
    /// <summary>
    ///  商店数据字典
    /// </summary>
    private Dictionary<string, shopData> shopListData;

    private float shopItemInitPosX = -1.5f;
    private float shopItemInitPosY = 55.17f;
    private float itemColumnSpace = 15;
    private float itemlineSpace = 25;
    private float imgItemBGPosX = -60f;


    ///////////// 页面逻辑处理 Control///////////
    public override void onStart()
    {
        base.onStart();
        // getShopData();// 之后抽出一层处理后台数据，传入当前页，当前页只处理页面逻辑。
        shopDataS2C _shopDataS2C = new shopDataS2C(testStr); // 之后数据层调用此方法。在页面初始化的时候，赋值该函数。    
        shopListData = _shopDataS2C.getShopList();
        SparkExtensions.SetChildText(_shopMenu.gameObject, "txt_userName", GlobalDataManager.instance.playerData.userName);
        SparkExtensions.SetChildText(_shopMenu.gameObject, "txt_price", GlobalDataManager.instance.playerData.price.ToString());
        SparkExtensions.SetChildText(_shopMenu.gameObject, "txt_score", GlobalDataManager.instance.playerData.score.ToString());
        initShopView();
        //NotificationCenter.DefaultCenter().AddObserver(this, "initShopView");
        //NotificationCenter.DefaultCenter().PostNotification(this, "initShopView",100000);

    }

    public override void OnClick(GameObject btn)
    {
        switch (btn.name)
        {
            case "btnShopClose":
                btn.gameObject.GetComponent<UIPlaySound>().Play();
                UIManager.Instance.unpopMenu();
                break;
            case "btnBuyProp":

                break;
            default:
                break;
        }
    }
    ///////////// 页面显示处理 View///////////

    // 初始化显示
    public void initShopView()
    {
        int pageNumber = 1; //shopListData.Count;
        Object shopItemBg = Resources.Load("Client/UI640/shopItemBg");
        for (int i = 0; i < pageNumber; i++)
        {
            GameObject scroll = _shopMenu.transform.Find("Scroll_0").gameObject;
            int itemNumber = shopListData["shopTab" + i].itemListData.Count;
            for (int j = 0; j < itemNumber; j++)
            {
                GameObject _shopItemBg = (GameObject)Instantiate(shopItemBg);
                float bgHeight = _shopItemBg.GetComponent<UI2DSprite>().height;
                float bgWidth = _shopItemBg.GetComponent<UI2DSprite>().width;
                _shopItemBg.transform.name = "btnBuy" + j;
                GameObject itemIcon = _shopItemBg.FindChild("imgBuy1_icon");
                itemIcon.AddComponent<UI2DSprite>();
                itemIcon.transform.localPosition = new Vector3(imgItemBGPosX, _shopItemBg.transform.localPosition.y, 0);
                SetImgForObj(itemIcon.transform, path + shopListData["shopTab" + i].itemListData["itemNum" + j].itemid);
                SparkExtensions.setParent(_shopItemBg, scroll, shopItemInitPosX + (bgWidth + itemColumnSpace) * (j % 2), shopItemInitPosY - (bgHeight + itemlineSpace) * (j / 2));
                SparkExtensions.SetChildText(_shopItemBg, "txt_buyQB", shopListData["shopTab" + i].itemListData["itemNum" + j].itemName);
                SparkExtensions.SetChildText(_shopItemBg, "txt_price", shopListData["shopTab" + i].itemListData["itemNum" + j].price.ToString());
                SparkExtensions.SetChildText(_shopItemBg, "txt_Buymon", shopListData["shopTab" + i].itemListData["itemNum" + j].itemDes);
            }
        }

    }
    private string testStr = @"
                     {
        ""ret"": 0,
        ""list"": [
            {
                ""page"": 1,
                ""pageName"": ""gamecoin"",
                ""list"": [
                    {
					
					    ""itemid"": 1,
                        ""itemName"": ""啦啦啦啦"",
                        ""itemDes"": ""厚度撒大家还是卡等"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 2,
                        ""itemName"": ""啦啦啦"",
                        ""itemDes"": ""厚度撒大家还是卡"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 3,
                        ""itemName"": ""啦啦啦啦啊"",
                        ""itemDes"": ""发货时"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 4,
                        ""itemName"": ""啦啦"",
                        ""itemDes"": ""是分开就撒了"",
                        ""price"": 100
				
					},
                    {
					
					  ""itemid"": 5,
                        ""itemName"": ""倒萨的打算"",
                        ""itemDes"": ""的方式将覆盖"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 6,
                        ""itemName"": ""发到手机看房"",
                        ""itemDes"": ""日饿哦热家里"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 7,
                        ""itemName"": ""发的啥看法"",
                        ""itemDes"": ""的时刻拉到了"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 8,
                        ""itemName"": ""服务商"",
                        ""itemDes"": ""健康管理局"",
                        ""price"": 100
				
					},
                    {
                        ""itemid"": 8,
                        ""itemName"": ""服务商"",
                        ""itemDes"": ""健康管理局"",
                        ""price"": 100
				
					},
                    {
                        ""itemid"": 8,
                        ""itemName"": ""服务商"",
                        ""itemDes"": ""健康管理局"",
                        ""price"": 100
				
					},
                    {
                        ""itemid"": 8,
                        ""itemName"": ""服务商"",
                        ""itemDes"": ""健康管理局"",
                        ""price"": 100
				
					}
                ]
            },
            {
                ""page"": 2,
                ""pageName"": ""prop"",
                ""list"": [
                    {
					
					  ""itemid"": 5,
                        ""itemName"": ""倒萨的打算"",
                        ""itemDes"": ""的方式将覆盖"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 6,
                        ""itemName"": ""发到手机看房"",
                        ""itemDes"": ""日饿哦热家里"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 7,
                        ""itemName"": ""发的啥看法"",
                        ""itemDes"": ""的时刻拉到了"",
                        ""price"": 100
                    },
                    {
                        ""itemid"": 8,
                        ""itemName"": ""服务商"",
                        ""itemDes"": ""健康管理局"",
                        ""price"": 100
				
					}
                    

                ]
            }
               ]
          
        }
       ";

}

